package com.ysm.smfu.adapter;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysm.smfu.MainActivity;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.CategoryActivity;
import com.ysm.smfu.activity.FoundActivity;
import com.ysm.smfu.food.FoodMainActivity;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;

/**
 * Created by Administrator on 2016/11/7.
 */
class MyGridAdapter extends BaseAdapter {

    private final String[] names;
    private final int[] ims;
    private final String[] ids;
    private final FragmentActivity fragmentActivity;

    public MyGridAdapter(String[] category_name2, int[] category_ims2, String[] category_id2, FragmentActivity fragmentActivity) {
        names = category_name2;
        ims = category_ims2;
        ids = category_id2;
        this.fragmentActivity = fragmentActivity;
    }

    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        View view1 = View.inflate(UIUtils.getContext(), R.layout.item_category_item, null);
        ImageView imageView = (ImageView) view1.findViewById(R.id.item_category_img);
        TextView text = (TextView) view1.findViewById(R.id.item_category_text);
        imageView.setImageResource(ims[i]);
        text.setText(names[i]);
        view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchForCategory(ids[i], names[i]);
            }
        });
        return view1;
    }

    /**
     * 分类搜索
     */
    private void searchForCategory(String id, String type) {
        if (TextUtils.equals(id, "0")) {
            ToastUtils.showToast("该功能暂未开放");
            return;
        } else if (TextUtils.equals(id, "-1")) {
//          精品商场
            toJingPinSC();
            return;
        } else if (TextUtils.equals(id, "-2")) {
//            兑换券商城
            toGouWuSC();
            return;
        } else if (TextUtils.equals(id, "-3")) {
//            聊天
            if (fragmentActivity instanceof MainActivity)
                ((MainActivity) fragmentActivity).setCurrItem();
            if (fragmentActivity instanceof FoundActivity) {
                sendBroadCastToChat(fragmentActivity);
            }
            return;
        }
//        else if (TextUtils.equals(id, "1")) {
////            跳往美食主页
//            toFoodMainPage();
//        }
        else
            toCategoryActivity(id, type);
    }

    private void toFoodMainPage() {
        UIUtils.startActivity(FoodMainActivity.class);
    }

    /**
     * 发送广播到聊天页面
     *
     * @param fragmentActivity
     */
    private void sendBroadCastToChat(FragmentActivity fragmentActivity) {
        Intent intent = new Intent();
        intent.setAction("tochatM");
        fragmentActivity.sendBroadcast(intent);
        fragmentActivity.finish();
    }

    private void toGouWuSC() {
        UIUtils.startActivity(FoundActivity.class, "found", 3);
    }

    private void toJingPinSC() {
        UIUtils.startActivity(FoundActivity.class, "found", 1);
    }

    private void toCategoryActivity(String id, String type) {
        Intent intent = new Intent(UIUtils.getContext(), CategoryActivity.class);
        intent.putExtra("typeId", id);
        intent.putExtra("type", type);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//指定任务栈
        UIUtils.getContext().startActivity(intent);
    }
}

